package com.qqtech.qquan.circle.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.model.CircleMemberGrade;
import com.qqtech.qquan.circle.model.CircleMemberGradeQuery;
import com.qqtech.qquan.circle.model.CircleMemberGradeVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.circle.service.CircleMemberGradeService;
import com.qqtech.qquan.circle.service.CircleMemberService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/circle/member/grade")
public class CircleMemberGradeController extends BaseWebController<CircleMemberGrade, CircleMemberGradeQuery> {
	@Resource
	private CircleMemberGradeService circleMemberGradeService;
	@Resource
	private CircleMemberService circleMemberService;
	@Resource
	private CircleBaseService circleBaseService;
	
	@Override
	protected BaseService<CircleMemberGrade> getBaseService() {
		return circleMemberGradeService;
	}
	
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody CircleMemberGradeQuery memberGradeQ) {
		Result result = new Result();
		Integer id = memberGradeQ.getId();
		if(id==null){
			result.addErr("等级id为空");
			return result;
		}
		CircleMemberGrade memberGrade = circleMemberGradeService.getById(id);
		result.put("memberGrade", memberGrade);
		return result;
	}
	
	/**
	 * 查询一个圈子的成员等级列表
	 * @param circleMemberGrade
	 * @return
	 */
	@RequestMapping(value = "/list4circle")
	@ResponseBody
	public Result list4circle(@RequestBody CircleMemberGradeQuery memberGradeQ) {
		Result result = new Result();
		Pageable pageKit = new PageKit(1, Integer.MAX_VALUE, new PageOrder(Direction.DESC, "gradeCode"));
		List<CircleMemberGradeVo> memberGradeList = circleMemberGradeService.queryList(memberGradeQ, pageKit);
		if(memberGradeList== null || memberGradeList.size()==0){
			int presetCount = circleMemberGradeService.copyPresetGrade2Circle(memberGradeQ.getCircleId());//复制预设“等级头衔”模板
			if(presetCount<=0){
				result.addErr("没有预设的【等级头衔】");
				return result;
			}
			memberGradeList = circleMemberGradeService.queryList(memberGradeQ, pageKit);
		}
		result.put("gradeList", memberGradeList);
		return result;

	}
	
	/**
	 * 启用一个成员等级代码
	 * @param memberGradeQ
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/setGradeEnable")
	@ResponseBody
	public Result setGradeEnable(@RequestBody CircleMemberGradeQuery memberGradeQ) {
		Result result = new Result();
		Integer circleId = memberGradeQ.getCircleId();
		Integer gradeCode = memberGradeQ.getGradeCode();
		if(circleId==null){
			result.addErr("圈子id 为空！");
			return result;
		}
		if(gradeCode==null){
			result.addErr("等级代码 为空！");
			return result;
		}
		Integer curUserId = super.getUserId();
		if(!circleMemberService.isCircleManager(curUserId, circleId) && !circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("你不是圈主或管理员！");
			return result;
		}
		circleMemberGradeService.setGradeUsed(circleId, gradeCode, true);
		return result;

	}
	
	/**
	 * 停用一个成员等级代码
	 * @param memberGradeQ
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/setGradeDisable")
	@ResponseBody
	public Result setGradeDisable(@RequestBody CircleMemberGradeQuery memberGradeQ) {
		Result result = new Result();
		Integer circleId = memberGradeQ.getCircleId();
		Integer gradeCode = memberGradeQ.getGradeCode();
		if(circleId==null){
			result.addErr("圈子id 为空！");
			return result;
		}
		if(gradeCode==null){
			result.addErr("等级代码 为空！");
			return result;
		}
		if(circleMemberGradeService.isHasGrant2Member(circleId, gradeCode)){
			result.addErr("该等级已经赋给成员,请先删除这些成员的等级头衔.");
			return result;
		}
		Integer curUserId = super.getUserId();
		if(!circleMemberService.isCircleManager(curUserId, circleId) && !circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("你不是圈主或管理员！");
			return result;
		}
		circleMemberGradeService.setGradeUsed(circleId, gradeCode, false);
		return result;

	}
	
	/**
	 * 修改等级（这里只能改名称）
	 * @param memberGrade
	 * @return
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/update")
	@ResponseBody
	public Result update(@RequestBody CircleMemberGrade memberGrade) {
		Result result = new Result();
		Integer id = memberGrade.getId();
		CircleMemberGrade memberGradeDb = circleMemberGradeService.getById(id);
		if(memberGradeDb==null){
			result.addErr("没有这个等级！");
			return result;
		}
		Integer circleId = memberGradeDb.getCircleId();
		Integer curUserId = super.getUserId();
		if(!circleMemberService.isCircleManager(curUserId, circleId) && !circleBaseService.isCircleCreator(curUserId, circleId)){
			result.addErr("你不是圈主或管理员！");
			return result;
		}
		CircleMemberGrade memberGrade4update = new CircleMemberGrade();
		memberGrade4update.setId(id);
		memberGrade4update.setGradeName(memberGrade.getGradeName());
		circleMemberGradeService.updateById(memberGrade4update);
		return result;

	}
}
